package org.keyyond.ms.business.service.impl;


import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.keyyond.ms.business.common.CommonService;
import org.keyyond.ms.business.dao.MsBusinessDocumentDao;
import org.keyyond.ms.business.model.MsBusinessDocument;
import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.service.MsBusinessDocumentService;
import org.keyyond.ms.business.util.Constant;
import org.keyyond.ms.core.exception.FDFBexception;
import org.keyyond.ms.core.util.JsonUtil;
import org.keyyond.ms.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MsbusinessDocumentServiceImpl extends CommonService implements MsBusinessDocumentService {
	
	@Autowired
	private MsBusinessDocumentDao documentDao;

	@Override
	public int addDocument(MsBusinessDocument record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public MsBusinessDocument getByKey(Long id) {
		// TODO Auto-generated method stub
		return documentDao.selectByPrimaryKey(id);
	}

	@Override
	public String updateByPrimaryKeySelective(String param) {
		try {
			//判断是否为空
			if(StringUtils.isEmpty(param) || param.equals("{}")) return "{\"fail\":\"失败,传入的参数为空\"}";

//			
			//将Json对象转化为JavaBean对象
			Map<String, Object> paramMap = JsonUtil.readJson2Map(JSONObject.fromObject(param).toString());
			if(paramMap == null) throw new FDFBexception("将传入的Json字符串转换成Map失败");
			//调用校验属性值的方法
			MsBusinessDocument docYear = new MsBusinessDocument();
			MsBusinessDocument docMonth = new MsBusinessDocument();
			docYear.setDocumentid(101l);
			docYear.setDocname((String)paramMap.get("yearDay"));
			docMonth.setDocumentid(102l);
			docMonth.setDocname((String)paramMap.get("monthDay"));
			
			//调用数据层添加的方法
			documentDao.updateByPrimaryKeySelective(docYear);
			documentDao.updateByPrimaryKeySelective(docMonth);
			return "{\""+ docYear.getDocumentid() +"\":\"成功\"}";
		} catch (Exception e) {
			e.printStackTrace();
			//记录日志
			MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
			messageLog.setLogtype(Constant.CONST_LOGTYPE_EXCEPTION);
			messageLog.setContent(StringUtils.isEmpty(param) ? "" : StringUtil.cutString(param, 1024, "GBK"));//截取长度,推送参数
			messageLog.setReturnmessage(StringUtil.cutString(e.getMessage(), 1024, "GBK"));//截取长度,返回消息
			messageLog.setCreateby(Constant.CONST_SYSTEM);//创建人
			messageLog.setModifyby(Constant.CONST_SYSTEM);//修改人
			
			return "{\"exception\":\"" + e.getMessage() + "\"}";
		}
	}

	@Override
	public MsBusinessDocument selectByDocCode(String docCode) {
		// TODO Auto-generated method stub
		return documentDao.selectByDocCode(docCode);
	}

	

	

	
	
}
